jQuery/Javascript Cookies and variable returning with value [object Object]
Posted
by
user1706661
on Stack Overflow
See other posts from Stack Overflow
or by user1706661
Published on 2012-09-28T15:35:39Z
Indexed on
2012/09/28
15:37 UTC
Read the original article
Hit count: 144
I am attempting to set a cookie to a site using jQuery, ONLY if the user came from a specific site. In this case, lets use -http://referrersite.com- as the site they must come from for the cookie to be created as an example. The cookie value is being stored in a variable and everything up to this point is working fine.
There is a conditional statement checking whether the user came from the referred site, if the cookie exists already and if the cookie doesn't exist and the user did not come from the referred site. If the user came from the referred site the cookie is created and stored in a variable. If the cookie already exists, it is then stored in a variable. If the cookie does not exist and the user did not come from the referred site I am assigning the variable a static string of characters - this is where the issue lies.
When the variable is alerted from the non referred site and no existing cookie, it returns: [object Object], not the static string of characters.
The code I am using is below:
$(document).ready(function() {
var referrer = document.referrer;
if(referrer == "http://referrersite.com") {
$.cookie("code","123456", { expires: 90, path: '/' });
cookieContainer = $.cookie("code");
alert(cookieContainer);
} else if($.cookie("code")) {
cookieContainer = $.cookie("code");
alert(cookieContainer);
} else if($.cookie("code") == null && referrer != "http://referrersite.com") {
cookieContainer = "67890";
alert(cookieContainer);
}
});
Please let me know if there is something I am missing as the code to me looks like it should work.
Thanks!
© Stack Overflow or respective owner